﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;

namespace Acc.Lib
{
    public enum UserFlag
    {
        Vip = 1<< 1
    }

    public class UserException : Exception
    {
        public Int32 ErrorCode { get; private set; }

        public UserException(Int32 errorCode, String message) : base(message)
        {
            ErrorCode = errorCode;
        }
    }


    public class UserInfo
    {
        private readonly Verify.IVerifyLib _lib = new Verify.CVerifyLibClass();

        private Int32 _userId;
        private String _nickName;
        private String _userName;
        private Int32 _usrFlag;
        private Boolean _isVip;

        public Int32 UserId
        {
            get { return _userId; }
            set{ _userId = value; }
        }

        public String NickName
        {
            get { return _nickName; }
            set { _nickName = value; }
        }

        public String UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public Boolean IsVip
        {
            get
            {
                var vipFlag = _usrFlag & (int)UserFlag.Vip;
                _isVip =  vipFlag == (int)UserFlag.Vip;
                return _isVip;
            }
            set { _isVip = value; }
        }

        public UserInfo()
        {
            _lib.SetLoginIp(WebConfigurationManager.AppSettings["login.server"]);
        }

        public void Login(String username, String password)
        {
            var returnVal = _lib.LoginByName(username, password);
            if (returnVal != 0)
                throw new UserException(returnVal, "登录失败.");

            GetBaseInfo();
            GetVip();
            
        }

        public void GetInfoByUserId(Int64 usrid)
        {
            var ret = _lib.GetInfoById((int)usrid);
            if (ret != 0)
                throw new UserException(ret, "没有该用户");
            GetBaseInfo();
            GetVip();
        }

        public void GetInfoByUserName(String username)
        {
            var ret = _lib.GetInfoByName(username);
            if (ret != 0)
                throw new UserException(ret, "没有该用户");
            GetBaseInfo();
            GetVip();
        }

        /// <summary>
        /// 设置VIP
        /// </summary>
        /// <param name="usrid">该获取VIP的用户ID</param>
        public void SetVip(Int64 usrid)
        {
            GetInfoByUserId(usrid);
            var ret = _lib.SetUserFlag((int)usrid, _usrFlag | (int)UserFlag.Vip);
            if (ret != 0) throw new UserException(ret, "添加VIP失败.");
        }

        



        private void GetBaseInfo()
        {
            var returnVal = _lib.GetUserId_Name_MailInfo(out _userId, out _nickName, out _userName);
            if (returnVal != 0)
                throw new UserException(returnVal, "获取信息失败.");
        }

        private void GetVip()
        {
            var returnVal = _lib.GetUserFlag(_userId, out _usrFlag);
            if (returnVal != 0)
                throw new UserException(returnVal, "获取信息失败.");
           
        }

        
    }
}
